package yancy.modules.sys.entity;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import yancy.common.validator.group.AddGroup;
import yancy.common.validator.group.UpdateGroup;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Data
@TableName("sys_book")
public class SysBook {
    @TableId
    @NotNull(message = "书籍id不能为空", groups = {UpdateGroup.class})
    private Long bookId;
    @NotBlank(message = "name不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String name;
    @NotBlank(message = "version不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String version;
    @NotBlank(message = "vendor不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String vendor;
    @NotNull(message = "publicDate不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private Date publicDate;
    @NotBlank(message = "summary不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String summary;

    @NotNull(message = "total不能为空", groups = {UpdateGroup.class})
    private Integer total;
    private Integer available;

    private Integer status;

    private Long createUserId;
    private Date createTime;

    /**
     * 角色ID列表
     */
    @TableField(exist = false)
    private List<Long> authorIdList;

    @TableField(exist = false)
    private String authorIds;

    @TableField(exist = false)
    private List<Long> fileIdList;

    @TableField(exist = false)
    private String fileIds;
    @TableField(exist = false)
    private List<SysFile> fileList;


    public void setAuthorIds(String authorIds) {
        this.authorIds = authorIds;
    }

    public void decAvail(Long total) {
        int newAvail = this.available - total.intValue();
        if (newAvail < 0) throw new IllegalArgumentException("当前图书不足");
        this.available = newAvail;
    }
}
